<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect @GetCategoryId="GetCategoryId" :show="isShowtable"
    /></el-card>
    <div v-show="isShowtable">
      <el-card>
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!Category3id"
          @click="Addattr"
          >添加属性</el-button
        >
        <!-- //表格 展示数据 -->
        <el-table :data="attrlist" style="width: 100%" border>
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150">
          </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template v-slot="{ row, $index }">
              <el-tag
                type="success"
                v-for="(attrvalue, index) in row.attrValueList"
                :key="attrvalue.id"
                style="margin: 0px 10px"
                >{{ attrvalue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="属性操作" width="150">
            <template v-slot="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="editAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <div v-show="!isShowtable">
      <el-card>
        <el-form :inline="true" ref="form" :model="attrinfo" label-width="80px">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名称"
              v-model="attrinfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="el-iocn-plus"
          style="margin-left: 25px"
          :disabled="!attrinfo.attrName"
          @click="addattrvalue"
          >添加属性名</el-button
        >
        <el-button @click="isShowtable = true">取消</el-button>
        <el-table
          style="width: 100%; margin: 20px 0"
          border
          :data="attrinfo.attrValueList"
        >
          <el-table-column
            type="index"
            label="序号"
            width="100px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template v-slot="{ row, $index }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值"
                size="mini"
                v-if="row.flag"
                @blur="tolook(row)"
                @keyup.native.enter="tolook(row)"
                ref="input"
              ></el-input>
              <span v-else @click="toEdit(row)" style="display: block">{{
                row.valueName
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template v-slot="{ row, $index }">
              <el-popconfirm
                :title="`你确定要删除${row.valueName}吗？`"
                @onConfirm="deleteAttrValue($index)"
              >
                <el-button type="danger" size="mini" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 0px 25px">
          <el-button
            type="primary"
            @click="AddOrUpdataAttr"
            :disabled="attrinfo.attrValueList.length < 1"
            >保存</el-button
          >
          <el-button @click="isShowtable = true">取消</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
//按需引入可以深拷贝的一个插件
import Deepclone from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      Category1id: "",
      Category2id: "",
      Category3id: "",
      //商品属性列表
      attrlist: [],
      isShowtable: true,
      attrinfo: {
        attrName: "",
        attrValueList: [],
        categoryId: "",
        categoryLevel: "",
      },
    };
  },
  methods: {
    //得到子组件传过来的id数据
    GetCategoryId({ id, index }) {
      if (index == 1) {
        this.Category1id = id;
        this.Category2id = "";
        this.Category3id = "";
      } else if (index == 2) {
        this.Category2id = id;
        this.Category3id = "";
      } else {
        this.Category3id = id;
        //当三级id存在时，发送请求获得数据，展示数据
        this.getAttrList();
      }
    },
    //得到的商品属性列表
    async getAttrList() {
      const { Category1id, Category2id, Category3id } = this;
      let result = await this.$API.attr.reqAttrlist(
        Category1id,
        Category2id,
        Category3id
      );
      // console.log(result,'得到的商品属性列表');
      if (result.code == 200) {
        this.attrlist = result.data;
      }
    },
    //添加属性按钮的回调
    Addattr() {
      this.isShowtable = false;
      this.attrinfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.Category3id,
        categoryLevel: 3,
      };
    },
    //编辑属性按钮的回调
    editAttr(row) {
      this.isShowtable = false;
      this.attrinfo = Deepclone(row);
      this.attrinfo.attrValueList.forEach((element) => {
        this.$set(element, "flag", false);
      });
    },
    //添加属性名按钮的回调
    addattrvalue() {
      this.attrinfo.attrValueList.push({
        attrId: this.attrinfo.id,
        valueName: "",
        //作为input和span切换的标记
        flag: true,
      });
      this.$nextTick(function () {
        this.$refs.input.focus();
      });
    },
    //失去焦点时的回调
    tolook(row) {
      row.valueName = row.valueName.trim();
      if (row.valueName.trim() == "") {
        this.$message("请输入正常的属性值");
        //获得input焦点
        this.$refs.input.focus();
        return;
      }
      //判断是否有相同的属性值
      let result = this.attrinfo.attrValueList.some((item) => {
        //排除row 再进行循环
        if (item !== row) {
          return row.valueName == item.valueName;
        }
      });
      if (result) {
        this.$message("请输入不同的属性值");
        this.$refs.input.focus();

        return;
      }
      row.flag = false;
    },
    //返回input时的回调
    toEdit(row) {
      row.flag = true;
      //获得焦点
      this.$nextTick(function () {
        this.$refs.input.focus();
      });
    },
    //删除某个属性值的回调
    deleteAttrValue(index) {
      this.attrinfo.attrValueList.splice(index, 1);
    },
    //保存属性值的回调
    async AddOrUpdataAttr() {
      //整理数据
      this.attrinfo.attrValueList = this.attrinfo.attrValueList.filter(
        (item) => {
          if (item.valueName.trim() !== "") {
            return true;
          }
        }
      );
      try {
        let result = await this.$API.attr.reqAddOrUpdataAttr(this.attrinfo);
        if (result.code == 200) {
          this.$message({ type: "success", message: "保存成功" });
          this.getAttrList();
          this.isShowtable = true;
        }
      } catch (error) {
        this.$message({ type: "danger", message: "保存失败" });
      }
    },
  },
};
</script>

<style>
</style>